<?php

class Db
{
    static public function genFormEdit($table, $option = 1)
    {
        $result = db()->query('SHOW FIELDS FROM ' . $table);
        if (2 === $option) {
            echo '$row = array(<br />';
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
            {
                echo "'" . $row['Field'] . "' => '',<br />";
            }
            echo ');<br />';
            db()->free($result);
            return;
        }

        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            echo '$' . $row['Field'] . ' = Form::input(\'' . $row['Field'] . '\'';
            if ('NO' == $row['Null']) {
                echo ', \'ne\'';
            } else {
                echo ', \'\'';
            }
            echo ');<br />';
        }
        db()->free($result);
    }

    static public function update($table, $where, $ignoredFields = array())
    {
        $where = db()->buildWhere($where);

        $updatedFields = array();
        $result = db()->query('SHOW FIELDS FROM ' . $table);

        while ($row = mysql_fetch_array($result, MYSQL_NUM))
        {
            if (!in_array($row[0], $ignoredFields)) {
                $updatedFields[] = '`' . $row[0] . "` = '\" . mysql_escape_string($" . $row[0] . ") . \"'";
            }
        }

        db()->free($result);

        return 'Db::query("UPDATE ' . $table . ' SET ' . implode(', ', $updatedFields) . ' ' . $where . '");';
    }

    static public function insert($table, $ignoredFields = array())
    {
        $fields = array();
        $values = array();

        $updatedFields = array();
        $result = db()->query('SHOW FIELDS FROM ' . $table);

        while ($row = mysql_fetch_array($result, MYSQL_NUM))
        {
            if (!in_array($row[0], $ignoredFields)) {
                $fields[] = '`' . $row[0] . '`';
                $values[] = '$' . $row[0];
            }
        }

        db()->free($result);

        return 'Db::query("INSERT INTO ' . $table . ' (' . implode(', ', $fields) . ') VALUES (\'" . mysql_escape_string(' . implode(') . "\', \'" . mysql_escape_string(', $values) . ') . "\')");';
    }

    static public function delete($table, $where)
    {
        db()->delete($table, $where);
        return mysql_affected_rows();
    }

    static public function query($sql, $showErrors = 0)
    {
        if (1 === $showErrors) {
            $result = db()->query($sql);
            if (!$result) {
                echo 'Cannot execute: ', $sql, '<br />', mysql_error();
                $e = new Exception();
                echo nl2br($e->getTraceAsString());
            }
            return $result;
        } elseif (2 === $showErrors) {
            echo $sql;
        } else {
            $result = db()->query($sql);
            return $result;
        }
    }

    static public function getRows($fields, $table, $where = '', $order = '', $showErrors = 0)
    {
        $sql = "SELECT $fields FROM $table " . db()->buildWhere($where) . ' ' . db()->buildSort($order);
        if (1 === $showErrors) {
            echo $sql;
        } else {
            return db()->getAll($sql, MYSQL_ASSOC);
        }
    }

    static public function getDBRows($rows, $table, $where, $option)
    {
        $dbsql = "SELECT ".$rows." FROM ".$table." WHERE 1=1 ".$where;
        if ($option==2) {echo "$dbsql"; exit;}
        $dbresult=@mysql_query($dbsql);
        if ($dbresult == null){
            if($option==1) {$viewerr='';} else {$viewerr= "$dbsql <br><br><b>".mysql_error();}
            die($viewerr);
        }
        $rows = array();
        while ($row = mysql_fetch_array($dbresult, MYSQL_ASSOC))
        {
            $rows[] = $row;
        }
        mysql_free_result($dbresult);
        unset($dbsql);
        return $rows;
    }

    static public function getOneField($field, $table, $where = '')
    {
        return db()->getValue("SELECT $field FROM $table " . db()->buildWhere($where));
    }

    static public function getOneRow($fields, $table, $where = '')
    {
        return db()->getFirst("SELECT $fields FROM $table " . db()->buildWhere($where));
    }

    static public function getFirstRow($fields, $table, $where = '', $order='', $showErrors = 0)
    {
        return db()->getFirst("SELECT $fields FROM $table " . db()->buildWhere($where). ' ' . db()->buildSort($order));
    }
}